package se.thoka.twt.client.utils.validation;

import com.google.gwt.event.shared.GwtEvent;

/**
 * @author Thobias Karlsson, Thoka
 * @since 9 jan 2010
 */
public class ValidationEvent extends GwtEvent<ValidationHandler> {

	private final static Type<ValidationHandler> TYPE = new Type<ValidationHandler>();
	
	private final Object source;
	private final boolean valid;
	private final String description;

	/**
	 * Creates a new instance of {@link ValidationEvent}
	 * 
	 * @param source The source object
	 * @param valid Set <code>true</code> if valid
	 * @param description The error description, may be <code>null</code>
	 */
	public ValidationEvent(Object source, boolean valid, String description) {
		this.source = source;
		this.valid = valid;
		this.description = description;
	}
	
	/**
	 * @return <code>true</code> if valid
	 */
	public boolean isValid() {
		return valid;
	}
	
	/**
	 * @return The error description, may return <code>null</code>
	 */
	public String getDescription() {
		return description;
	}
	
	@Override
	public Object getSource() {
		return source;
	}

	@Override
	public Type<ValidationHandler> getAssociatedType() {
		return getType();
	}
	
	/**
	 * @return The event type
	 */
	public static Type<ValidationHandler> getType() {
		return TYPE;
	}
	
	@Override
	protected void dispatch(ValidationHandler handler) {
		handler.onValidationChanged(this);
	}
}
